<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="partnerId" autocomplete="off" class="layui-input"
                                       placeholder="请输入具体的ID">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">收货人姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="consigneeName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">手机号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="consigneePhoneNum" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">交付状态</label>
                            <div class="layui-input-inline">
                                <select id="joinStatus" name="joinStatus" lay-filter="orderStatus">
                                    <option value="">全部</option>
                                    <option value="未验收">未验收</option>
                                    <option value="验收中">验收中</option>
                                    <option value="已验收">已验收</option>
                                    <option value="已超时">已超时</option>
                                    <option value="已回收">已回收</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="deliverys"> 发货
                </button>
                <button class="layui-btn layui-btn-danger layui-btn-sm data-add-btn" lay-event="changeLogistics"> 改物流
                </button>
                <button class="layui-btn layui-btn layui-btn-sm data-add-btn" lay-event="batchImport"> 批量发货
                </button>
                <button class="layui-btn layui-btn-warm layui-btn-sm data-add-btn" lay-event="goodsDeliveryNotify">发送短信
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentSubTableId" lay-filter="currentSubTableFilter"></table>

    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../js/lay-module/xm-select-master/dist/xm-select.js" charset="utf-8"></script>
<script>

    let evaluationId = "";// 测评主文档id
    let subjectName = "";
    window.onload = function () {
        evaluationId = GetQueryString("evaluationId");
        subjectName = GetQueryString("subjectName");
        if (isNotNull(evaluationId)) {
            console.log("获取参数id=", evaluationId);

            layui.use(['form', 'table'], function () {
                var $ = layui.jquery,
                    form = layui.form,
                    table = layui.table;

                table.render({
                    elem: '#currentSubTableId',
                    url: '/trialEvaluation/joinList',
                    method: 'POST',
                    contentType: 'application/json',
                    toolbar: '#toolbarDemo',
                    where: {
                        "id": evaluationId
                    },
                    defaultToolbar: ['filter', 'exports', 'print', {
                        title: '提示',
                        layEvent: 'LAYTABLE_TIPS',
                        icon: 'layui-icon-tips'
                    }],
                    cols: [[
                        {type: "radio"},
                        {
                            field: 'numbering', title: '序号', width: 60, templet: function (d) {
                                return d.LAY_INDEX; //返回序号
                            }
                        },
                        {field: 'partnerId', title: '用户ID', hide: false, sort: true, align: 'center', width: 120},
                        {field: 'partnerName', title: '用户昵称', sort: true, align: 'center', width: 120},
                        {field: 'joinTime', title: '报名时间', sort: true, align: 'center', width: 120},
                        {field: 'orderStatus', title: '订单状态', sort: true, align: 'center', width: 120},
                        {field: 'joinStatus', title: '交付状态', sort: true, align: 'center', width: 120},
                        {field: 'deliveryTime', title: '提交时间', sort: true, align: 'center', width: 120},
                        {field: 'acceptanceTime', title: '验收时间', sort: true, align: 'center', width: 120},
                        {field: 'evaluatorLevel', title: '测评等级', sort: true, align: 'center', width: 120},
                        {field: 'rewards', title: '测评报酬', sort: true, align: 'center', width: 120},
                        {field: 'consigneeName', title: '收货人姓名', sort: true, align: 'center', width: 120},
                        {field: 'consigneePhoneNum', title: '手机号码', sort: true, align: 'center', width: 120},
                        {field: 'consigneeAddress', title: '收获地址', sort: true, align: 'center', width: 120},
                        {field: 'courierName', title: '物流公司', sort: true, align: 'center', width: 120},
                        {field: 'courierNumber', title: '物流单号', sort: true, align: 'center', width: 120},
                        {field: 'orderRemarks', title: '备注', sort: true, align: 'center', width: 120},

                    ]],
                    limits: [50, 100, 150, 200],
                    limit: 50,
                    page: true,
                    request: {
                        pageName: 'pageNum',
                        limitName: 'pageSize'
                    },
                    response: { //定义后端 json 格式，详细参见官方文档
                        statusName: 'code', //状态字段名称
                        statusCode: '200', //状态字段成功值
                        msgName: 'msg', //消息字段
                        countName: 'total', //总数字段
                        dataName: 'rows' //数据字段
                    },
                });

            });
        } else {
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        }
    }

    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.on('toolbar(currentSubTableFilter)', function (obj) {
            let checkStatus = table.checkStatus('currentSubTableId')
                , data = checkStatus.data;
            if (obj.event === 'batchImport') {
                const index = layer.open({
                    type: 2,
                    title: "批量发货",
                    content: '../common/import/importComponents.html',
                    area: ['274px', '140px'],
                    success: function (layero, index) {
                        // 下载模板请求参数
                        const param = {
                            batchIds: [],
                            batchType: 1,
                            actions: 'EVALUATION_BATCH_DELIVERY',
                            queryVO: {id: evaluationId},
                        }
                        // 获取子页面的iframe
                        var iframe = window['layui-layer-iframe' + index];
                        // 向子页面的全局函数child传参
                        let data = {
                            module: 'EVALUATION_BATCH_DELIVERY',
                            param: param,
                            templateName: subjectName + '测评发货模板.xlsx',
                            dataPreviewUrl: '../common/import/dataPreviewTable.html',
                            errTemplateName: subjectName + '测评发货失败数据.xlsx',
                        }
                        iframe.childParam(data);
                    },
                    end: function () {
                        //关闭弹框后触发
                        reloadTable()
                    }
                });
                return false;
            }
            var select_data = data[0];// 选中的
            if (select_data === void 0 || select_data === null) {
                layer.msg("请至少选择一行数据");
                return false;
            }
            if (obj.event === 'deliverys') {  // 监听发货操作
                if (select_data.orderStatus !== '待发货') {
                    layer.msg('已发货，无需再处理！');
                    return false;
                }
                var index = layer.open({
                    title: '快递信息填写页面',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['40%', '50%'],
                    content: './product-delivery.html?docUnid=' + select_data.docUnid + '&orderStatus=' + select_data.orderStatus,
                    end: function () {
                        //关闭弹框后触发
                        reloadTable()
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'changeLogistics') {
                let courierName = select_data.courierName;
                let courierNumber = select_data.courierNumber;
                if (select_data.orderStatus === '待发货') {
                    layer.msg('订单还未发货，请先发货');
                    return false;
                }
                if (select_data.orderStatus === '已完成') {
                    layer.msg('订单已完成，不能进行更改发货操作');
                    return false;
                }
                if (!isNotNull(courierName) || !isNotNull(courierNumber)) {
                    layer.msg('物流公司或物流单号为空，不能进行更改发货操作');
                    return false;
                }
                var index = layer.open({
                    title: '快递信息填写页面',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['40%', '50%'],
                    content: './product-delivery.html?docUnid=' + select_data.docUnid + '&orderStatus=' + select_data.orderStatus,
                    end: function () {
                        //关闭弹框后触发
                        reloadTable()
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'goodsDeliveryNotify') {
                let courierName = select_data.courierName;
                let courierNumber = select_data.courierNumber;
                if (select_data.orderStatus === '待发货') {
                    layer.msg('没有物流信息，请先发货');
                    return false;
                }
                if (select_data.orderStatus === '已完成') {
                    layer.msg('订单已完成，不能进行更改发货操作');
                    return false;
                }
                if (!isNotNull(courierName) || !isNotNull(courierNumber)) {
                    layer.msg('物流公司或物流单号为空，不能进行更改发货操作');
                    return false;
                }
                if (!isNotNull(subjectName)) {
                    layer.msg('数据有误，样品名称为空');
                    return false;
                }
                const requestData = {
                    sendTo: select_data.consigneeName,
                    phoneNumber: select_data.consigneePhoneNum,
                    commodity: subjectName,
                    company: select_data.courierName,
                    numbers: select_data.courierNumber
                }
                let index = layer.alert("确定向该用户发送发货通知？", {
                    title: '提示'
                }, function () {
                    let myMsg = layer.msg("正在处理...", {
                        icon: 16,
                        time: -1
                    })
                    $.ajax({
                        type: "post",
                        url: '/notify/send/goods/delivery',
                        data: JSON.stringify(requestData),
                        dataType: 'json',
                        contentType: 'application/json; charset=UTF-8',
                        success: function (req) {
                            if (req.code === '200') {
                                layer.msg('已发送');
                                layer.close(index);
                                // 关闭弹出层
                            } else {
                                layer.msg(req.msg);
                            }

                        },
                        error: function (e) {
                            layer.msg(e.msg);
                            layer.close(myMsg);//手动关闭
                        }
                    })

                });

            }
            return false;
        })


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentSubTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    id: evaluationId,
                    partnerId: data.field.partnerId,
                    consigneeName: data.field.consigneeName,
                    joinStatus: data.field.joinStatus,
                    consigneePhoneNum: data.field.consigneePhoneNum
                }
            }, 'data');

            return false;
        });
    });


    /**
     * 重载表格数据
     */
    function reloadTable() {
        layui.use(['table'], function () {
            let table = layui.table;
            table.reload('currentSubTableId', {
                url: '/trialEvaluation/joinList',
                where: {
                    "id": evaluationId
                },
            });
        })
    }

</script>

</body>
</html>